package com.example.androidui;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class Custom extends DialogFragment {
    private EditText editText;
    private EditText editText2;
    private Custom.NotifyDialogListener listener;

    public interface NotifyDialogListener {
        public void onDialogPositiveClicked(String editText, String editText2);
    }

    public void setListener(Custom.NotifyDialogListener listener) {
        this.listener = listener;
    }
    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = requireActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.activity_custom,null);
        editText = view.findViewById(R.id.editText);
        editText2 = view.findViewById(R.id.editText2);
        builder.setView(view)
                .setTitle("自定义对话框")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        listener.onDialogPositiveClicked(editText.getText().toString(),editText2.getText().toString());
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getActivity().getApplicationContext(),"你取消了！",Toast.LENGTH_SHORT).show();
                    }
                });
        return builder.create();
    }
}